package com.test;

import javax.portlet.GenericPortlet;
import javax.portlet.ActionRequest;
import javax.portlet.RenderRequest;
import javax.portlet.ActionResponse;
import javax.portlet.RenderResponse;
import javax.portlet.PortletException;
import java.io.IOException;
import java.net.URLEncoder;
import javax.portlet.PortletMode;
import javax.portlet.PortletPreferences;
import javax.portlet.PortletRequestDispatcher;
import javax.portlet.ValidatorException;


/**
 * JIRA_Description Portlet Class
 */
public class JIRA_Description extends GenericPortlet {

    public void processAction(ActionRequest request, ActionResponse response) throws PortletException,IOException {
        String error = null;
        String success = null;
        PortletPreferences pref = request.getPreferences();

        if(request.getPortletMode().equals(PortletMode.EDIT)){
            boolean editOk = false;
            String url = request.getParameter("formUrl");
            String login = request.getParameter("formLogin");
            pref.setValue("jiraUrl", url);
            /*if(login.equals("")){
                login ="null";
            }*/
            pref.setValue("jiraLogin", login);
            try{
                if(pref.getValue("jiraUrl", "").equals("null")||pref.getValue("jiraLogin", "null").equals("")){
                    throw new ValidatorException("Set your JIRA Login correctly", null);
                }else{
                    pref.store();
                    editOk = true;
                }
            }catch(ValidatorException ex){
                error = ex.getMessage();
            }
            if(editOk){
                success = "Your request processed successfully.";
                response.setRenderParameter("success", URLEncoder.encode(success, "UTF-8"));
            }else{
                response.setRenderParameter("error", URLEncoder.encode(error, "UTF-8"));
            }
        }
        if(request.getPortletMode().equals(PortletMode.VIEW)){
            String edit = request.getParameter("edit");
            if(edit.equals("link")){
                response.setPortletMode(PortletMode.EDIT);
            }else{
                //String filter = request.getParameter("vformFilter");
                //response.setRenderParameter("filter", filter); //send filter parameter to doView();
            }
        }
    }
    
    public void doView(RenderRequest request,RenderResponse response) throws PortletException,IOException {
        response.setContentType("text/html");
        PortletPreferences pref = request.getPreferences();
        String url = pref.getValue("jiraUrl", "null");
        String login = pref.getValue("jiraLogin", "null");

        request.setAttribute("JIRA_URL", url);
        request.setAttribute("JIRA_LOGIN", login);
        /*Boolean first;
        if(url.equals("null")){
            first = true;
        }else{
            first = false;
        }
        System.out.println("First is set" + first);
        if (!first) {
            String filter = request.getParameter("filter"); //get hudson project filter from processAction()
            if(filter==null){
                filter = pref.getValue("jiraLogin", "null"); //if view page doesn't specify project filter, get the filter from preferences
            }
            if(filter.equals("null")||filter.equals("")){
                filter="*";
            }

            //Driver projList = new Driver();
            //request.setAttribute("projList", projList.setProject(url, filter));
           // request.setAttribute("error", projList.getErrors());
           // request.setAttribute("filter", filter);
        } else {
        }*/
        //System.out.println("Going to call jira_viewjsp");
        PortletRequestDispatcher dispatcher =
        getPortletContext().getRequestDispatcher("/WEB-INF/jsp/JIRA_Description_view.jsp");
        dispatcher.include(request, response);
    }
    public void doEdit(RenderRequest request,RenderResponse response) throws PortletException,IOException {
            response.setContentType("text/html");        
        PortletRequestDispatcher dispatcher =
        getPortletContext().getRequestDispatcher("/WEB-INF/jsp/JIRA_Description_edit.jsp");
        dispatcher.include(request, response);
    }
    public void doHelp(RenderRequest request, RenderResponse response) throws PortletException,IOException {

        response.setContentType("text/html");        
        PortletRequestDispatcher dispatcher =
        getPortletContext().getRequestDispatcher("/WEB-INF/jsp/JIRA_Description_help.jsp");
        dispatcher.include(request, response);
    }
}